package com.app.couponsharelife.ui.adapter
import androidx.databinding.DataBindingUtil
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.TaobaoOrderBean
import com.app.couponsharelife.databinding.ActivityOrderItemSettlementBinding

/**
 * Created by guluwa on 2018/11/22.
 */
class OrderGoodAdapter(private val list: List<TaobaoOrderBean.RowsBean>) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return ViewHolder(DataBindingUtil.inflate(
                LayoutInflater.from(parent.context), R.layout.activity_order_item_settlement, parent, false))
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        if (holder is ViewHolder) {
            Glide.with(holder.itemView).asBitmap()
                    .apply(RequestOptions()
                            .placeholder(R.drawable.ic_good_place_holder)
                            .transform(RoundedCorners(10)))
                    .load(list[position].itemurl)
                    .into(holder.databinding.ivOrderImage)
            holder.databinding.tvOrderName.text = list[position].title
            holder.databinding.tvOrderCreateTime.text = String.format("创建时间 %s", list[position].createTime.substring(0, 11))
            holder.databinding.tvOrderMoney.text = String.format("%s元", list[position].price)
            holder.databinding.tvEvaluateProfit.text = String.format("%s元", list[position].estimatecome)
            if (list[position].orderstatus == "2") {
                holder.databinding.tvOrderSettleTime.text = String.format("结算时间 %s", list[position].settleTime.substring(0, 11))
                holder.databinding.tvOrderSettleTime.visibility = View.VISIBLE
            } else {
                holder.databinding.tvOrderSettleTime.visibility = View.INVISIBLE
            }
            if (list[position].orderstatus == "0") {
                holder.databinding.tvOrderName.setTextColor(holder.itemView.context.resources.getColor(R.color.gray99))
                holder.databinding.textView5.visibility = View.INVISIBLE
                holder.databinding.tvOrderMoney.visibility = View.INVISIBLE
                holder.databinding.estimateReward.visibility = View.INVISIBLE
                holder.databinding.tvEvaluateProfit.visibility = View.INVISIBLE
            } else {
                holder.databinding.tvOrderName.setTextColor(holder.itemView.context.resources.getColor(R.color.act_login_edit_text_text_color))
                holder.databinding.textView5.visibility = View.VISIBLE
                holder.databinding.tvOrderMoney.visibility = View.VISIBLE
                holder.databinding.estimateReward.visibility = View.VISIBLE
                holder.databinding.tvEvaluateProfit.visibility = View.VISIBLE
            }
        }
    }

    inner class ViewHolder(val databinding: ActivityOrderItemSettlementBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root)
}